package coo1;

import java.util.Random;

public class Grades {
    private int numerator, denominator;

    public Grades(int numerator,int denominator)
    {
        if(denominator == 0)
            denominator = 1;

        if(denominator < 0)
        {
            numerator = numerator * -1;
            denominator = denominator * -1;
        }

        this.numerator = numerator;
        this.denominator = denominator;

        reduce();
    }

    public int getNumerator()
    {
        return this.numerator;
    }

    public int getDenominator()
    {
        return this.denominator;
    }

    public Grades add(Grades op2)
    {
        int commonDenominator = this.denominator * op2.getDenominator();
        int numerator1 = this.numerator * op2.getDenominator();
        int numerator2 = this.numerator * op2.getDenominator();
        int sum = numerator1 + numerator2;

        System.out.print("(" + this.toString() + ")" + " + " + "(" + op2.toString() + ")" + " ＝ ");
        return new Grades(sum,commonDenominator);
    }

    public Grades subtract(Grades op2)
    {
        int commonDenominator = this.denominator * op2.getDenominator();
        int numerator1 = this.numerator * op2.getDenominator();
        int numerator2 = this.numerator * op2.getDenominator();
        int different = numerator1 - numerator2;

        System.out.print("(" + this.toString() + ")" + " - " + "(" + op2.toString() + ")" + " ＝ ");
        return new Grades(different,commonDenominator);
    }

    public Grades multiply(Grades op2)
    {
        int numer = this.numerator * op2.getNumerator();
        int denom = this.denominator * op2.getDenominator();

        System.out.print("(" + this.toString() + ")" + " * " + "(" + op2.toString() + ")" + " ＝ ");
        return new Grades(numer,denom);
    }

    public Grades divide(Grades op2)
    {
        int numer = this.numerator * op2.getDenominator();
        int denom = this.denominator * op2.getNumerator();

        System.out.print("(" + this.toString() + ")" + " / " + "(" + op2.toString() + ")" + " ＝ ");
        return new Grades(numer,denom);
    }

    public boolean isLike(Grades op2)
    {
        return (this.numerator == op2.getNumerator() &&
                this.denominator == op2.getDenominator());
    }

    public String toString()
    {
        String result;
        if (this.numerator == 0)
            result = "0";
        else
        if (this.denominator == 1)
            result = this.numerator + "";
        else
            result = this.numerator + "/" + this.denominator;

        return result;
    }

    private void reduce()
    {
        if (this.numerator != 0)
        {
            int common = gcd(Math.abs(this.numerator),this.denominator);

            this.numerator = this.numerator / common;
            this.denominator = this.denominator / common;
        }
    }

    private int gcd(int num1, int num2)
    {
        while (num1 != num2)
            if (num1 > num2)
                num1 = num1 - num2;
            else
                num2 = num2 - num1;

        return num1;
    }

    public static Grades obj(){
        Random generator = new Random();
        return new Grades(generator.nextInt(51) - 50,generator.nextInt(51) - 50 );
    }
}
